import { Draw } from "../../../Draw/Draw";

/** 外部拖拽文件、图片 */
export async function containerDrop(e: DragEvent, draw: Draw) {
  e.stopPropagation();
  e.preventDefault();

  const konvaGraph = draw.getKonvaGraph();

  const data = e.dataTransfer?.getData("text");

  if (data) {
    // 拖拽的文本，应该创建 konvaGraph Text 对象
    const text = konvaGraph.Text({
      text: data,
      width: 120,
      height: 20,
      x: e.offsetX,
      y: e.offsetY,
    });
    konvaGraph.addShape({ shape: text });
  } else {
    const files = e.dataTransfer?.files;
    if (!files) return;
    for (let i = 0; i < files.length; i++) {
      const file = files[i];
      if (file.type.startsWith("image")) {
        const fileImage = await konvaGraph.Image({
          source: file,
          x: e.offsetX,
          y: e.offsetY,
        });
        konvaGraph.addShape({ shape: fileImage });
      }
      if (file.type.startsWith("video")) {
        const fileVideo = await konvaGraph.Video({
          source: file,
          image: undefined,
          x: e.offsetX,
          y: e.offsetY,
        });
        konvaGraph.addShape({ shape: fileVideo });
      }
    }
  }
}
